package hvzt.minimap.asynctasks;

import hvzt.minimap.activities.ServerActivity;
import iface.Envelope;
import iface.Kill;
import iface.MessageInterface;
import iface.Status;
import android.os.AsyncTask;

/**
 * KillTask sends a Kill message to the server, calling methods on the UI thread to update
 * progress and handle the result from the server.
 * @author Franklin Pearsall
 *
 */
public class KillTask extends AsyncTask<Kill, Progress, Status> {
	
	private ServerActivity m_s;
	
	public KillTask(ServerActivity s) {
		m_s = s;
	}

	/**
	 * Sends a kill message to the server and returns the response.
	 */
	@Override
	protected iface.Status doInBackground(Kill... params) {
		publishProgress(Progress.IN_PROGRESS);
		Kill k = params[0];
		String user = "test";
		Envelope env = new Envelope(user, k);
		iface.Status status = MessageInterface.sendEnvelopeToServer(env);
		publishProgress(Progress.DONE);
		return status;
	}

	/**
	 * Hands the response from the server to the Activity.
	 */
	@Override
	protected void onPostExecute(iface.Status result) {
		m_s.handleResult(result);
		super.onPostExecute(result);
	}

	/**
	 * Updates the progress on the UI.
	 */
	@Override
	protected void onProgressUpdate(Progress... values) {
		Progress p = values[0];
		m_s.updateProgress(p);
		super.onProgressUpdate(values);
	}

}
